#!/bin/sh
sctglist=$1
illuminadir=/host/Users/JS/UH-work/gloeobacter/final_assembly

#Flow of processes
#Run MUMMER to recruit Illumina reads
#Extract Illumina reads
#Run Minimo from AMOS package
#Run MUMMER to see if consensus bases changed or not

for i in `cat $sctglist`;do
    nucmer $i.fasta $illuminadir/B_trimmed.1.fasta -p $i.b1
    nucmer $i.fasta $illuminadir/B_trimmed.2.fasta -p $i.b2
    show-coords -r -c -l -T $i.b1.delta | grep ILLUMINA | awk '{print $13}' > $i.b1.list
    show-coords -r -c -l -T $i.b2.delta | grep ILLUMINA | awk '{print $13}' > $i.b2.list
    extract_fasta_by_list.py $illuminadir/B_trimmed.1.fasta $i.b1.list > $i.b1.fasta
    extract_qual_by_list.py $illuminadir/B_trimmed.1.qual $i.b1.list > $i.b1.qual
    extract_fasta_by_list.py $illuminadir/B_trimmed.2.fasta $i.b2.list > $i.b2.fasta
    extract_qual_by_list.py $illuminadir/B_trimmed.2.qual $i.b2.list > $i.b2.qual
    cat $i.fasta $i.b1.fasta $i.b2.fasta > $i.toimprove.fasta
    cat $i.qual $i.b1.qual $i.b2.qual > $i.toimprove.qual
    Minimo $i.toimprove.fasta -D QUAL_IN=$i.toimprove.qual -D MIN_LEN=20 -D FASTA_EXP=1 -D OUT_PREFIX=$i.toimprove
    improvedctg=`count.fasta.py $i.toimprove-contigs.fa | sort -nr -k 2 | head -1 | awk '{print $1}'`
    exfasta $improvedctg $i.toimprove-contigs.fa > $i.improvedctg.fasta
    exfasta $improvedctg $i.toimprove-contigs.qual > $i.improvedctg.qual
    sed -i 's/>.*/>'$i'_improved/g' $i.improvedctg.fasta
    sed -i 's/>.*/>'$i'_improved/g' $i.improvedctg.qual
done
